package com.atguigu.lease.web.admin.custom.config;

import com.atguigu.lease.web.admin.custom.converter.StringToBaseEnumConverterFactory;
import com.atguigu.lease.web.admin.interceptor.AuthenticationAdminInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.format.FormatterRegistry;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/***
 * 自定义枚举转换器配置类
 * 将转换器注册到IOC容器中，以便在全局范围内使用
 */
@Configuration
@PropertySource("classpath:application.yml")
public class WebMvcConfiguration implements WebMvcConfigurer{
    @Autowired
    private StringToBaseEnumConverterFactory stringToBaseEnumConverterFactory;

    @Autowired
    private AuthenticationAdminInterceptor authenticationAdminInterceptor;

    @Value("${admin.auth.path-patterns.include}")
    private String addPathPatterns;//拦截谁，路径
    @Value("${admin.auth.path-patterns.exclude}")
    private String excludePathPatterns;//不拦截谁，路径

    /**
     * 添加自定义的转换器
     * 将自定义的转换器添加到转换器的注册表中，以便在全局范围内使用
     * @param registry
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
       registry.addConverterFactory(stringToBaseEnumConverterFactory);
    }

    /**
     * 添加自定义的拦截器
     * 将自定义的拦截器添加到拦截器的注册表中，以便在全局范围内使用
     * 指定拦截的路径和排除的路径
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authenticationAdminInterceptor)
                .addPathPatterns(addPathPatterns)
                .excludePathPatterns(excludePathPatterns);
    }
}
